基于唯一观察结果筛选数据表输出

huangapple go评论58阅读模式
英文:

Filter DataTable Output based on unique observations

问题

Sure, here is the translated code:

以下是我的闪亮应用程序的代码

    library(shiny)
    
    DF = data.frame(ID = c("1","2","3","4","5"),
                    product = c("A","B","C","A","C"),
                    sentiment = c("好","中","差","中","差"),
                    online = c(1,0,1,1,0),
                    ooh = c(0,1,0,1,1),
                    Date= c("2020-08-11", "2020-09-16","2021-10-20", "2020-11-25", "2022-11-27"), 
                    event = c(1,1,0,0,0))
    
    # 定义绘制直方图的应用程序的UI
    ui <- fluidPage(
      
      # 应用程序标题
      titlePanel("产品情感"),
      
      # 侧边栏带有用于选择箱数的滑块输入
      sidebarLayout(
        sidebarPanel(
          selectInput("selectsentiment", label = "选择情感", choices = unique(DF$sentiment)), 
          dateRangeInput("daterange", h5("日期范围"), 
                         start = ymd("2020-08-11"), 
                         end = ymd("2022-09-06"), 
                         min = min(DF$Date), 
                         max = max(DF$Date),
                         format = "yyyy-mm-dd"
          ) 
        ),
        
        # 显示生成的分布的图表
        mainPanel(
          dataTableOutput("DT_Table")
        )
      )
    )
    
    # 定义绘制直方图所需的服务器逻辑
    server <- function(input, output) {
      
      output$DT_Table <- renderDataTable({ 
        DF[DF$sentiment %in% input$selectsentiment & 
           DF$Date >= input$daterange[1] & 
           DF$Date <= input$daterange[2],]
      })   
    }
    
    # 运行应用程序
    shinyApp(ui = ui, server = server)
英文:

Below is a code to my shiny app

library(shiny)
DF = data.frame(ID = c (&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;),
product = c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;A&quot;,&quot;C&quot;),
sentiment = c(&quot;good&quot;,&quot;medium&quot;,&quot;bad&quot;,&quot;medium&quot;,&quot;bad&quot;),
online = c(1,0,1,1,0),
ooh = c(0,1,0,1,1),
Date= c(&quot;2020-08-11&quot;, &quot;2020-09-16&quot;,&quot;2021-10-20&quot;, &quot;2020-11-25&quot;, &quot;2022-11-27&quot;), 
event = c(1,1,0,0,0))
# Define UI for application that draws a histogram
ui &lt;- fluidPage(
# Application title
titlePanel(&quot;Product Sentiment&quot;),
# Sidebar with a slider input for number of bins 
sidebarLayout(
sidebarPanel(
selectInput(&quot;selectsentiment&quot;, label = &quot;Select sentiment&quot;, choices = unique(DF$sentiment)), 
dateRangeInput(&quot;daterange&quot;, h5(&quot;Date range&quot;), 
start = ymd(&quot;2020-08-11&quot;), 
end = ymd(&quot;2022-09-06&quot;), 
min = min(DF$Date), 
max = max(DF$Date),
format = &quot;yyyy-mm-dd&quot;
) 
),
# Show a plot of the generated distribution
mainPanel(
dataTableOutput(&quot;DT_Table&quot;)
)
)
)
# Define server logic required to draw a histogram
server &lt;- function(input, output) {
output$DT_Table &lt;- renderDataTable({ 
DF[DF$sentiment %in% input$selectsentiment,]
})   
}
# Run the application 
shinyApp(ui = ui, server = server)

I want to filter the output of the datatable when a person does both. I.e. selecting positive or negative as well as selecting a date date range
How do I go about that?

答案1

得分: 0

请检查更新后的renderDataTable调用:

library(shiny)

DF = data.frame(ID = c("1","2","3","4","5"),
                product = c("A","B","C","A","C"),
                sentiment = c("good","medium","bad","medium","bad"),
                online = c(1,0,1,1,0),
                ooh = c(0,1,0,1,1),
                event = c(1,1,0,0,0))



# 定义应用程序的UI
ui <- fluidPage(

  # 应用程序标题
  titlePanel("Product Sentiment"),

  # 侧边栏,用于选择柱数的滑块输入
  sidebarLayout(
    sidebarPanel(
      selectInput("selectsentiment", label = "Select sentiment", choices = unique(DF$sentiment))
    ),

    # 显示生成的分布的图表
    mainPanel(
      dataTableOutput("DT_Table")
    )
  )
)

# 定义绘制直方图所需的服务器逻辑
server <- function(input, output) {

  output$DT_Table <- renderDataTable({ 
    DF[DF$sentiment %in% input$selectsentiment,]
  })   

}

# 运行应用程序
shinyApp(ui = ui, server = server)
英文:

Please check the updated renderDataTable call:

library(shiny)
DF = data.frame(ID = c (&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;),
product = c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;A&quot;,&quot;C&quot;),
sentiment = c(&quot;good&quot;,&quot;medium&quot;,&quot;bad&quot;,&quot;medium&quot;,&quot;bad&quot;),
online = c(1,0,1,1,0),
ooh = c(0,1,0,1,1),
event = c(1,1,0,0,0))
# Define UI for application that draws a histogram
ui &lt;- fluidPage(
# Application title
titlePanel(&quot;Product Sentiment&quot;),
# Sidebar with a slider input for number of bins 
sidebarLayout(
sidebarPanel(
selectInput(&quot;selectsentiment&quot;, label = &quot;Select sentiment&quot;, choices = unique(DF$sentiment))
),
# Show a plot of the generated distribution
mainPanel(
dataTableOutput(&quot;DT_Table&quot;)
)
)
)
# Define server logic required to draw a histogram
server &lt;- function(input, output) {
output$DT_Table &lt;- renderDataTable({ 
DF[DF$sentiment %in% input$selectsentiment,]
})   
}
# Run the application 
shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年4月17日 15:52:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定