【问题标题】:R Shiny: Computing new Variables selected by "selectInput"R Shiny:计算“selectInput”选择的新变量
【发布时间】:2018-02-14 23:02:44
【问题描述】:

我正在使用 Shiny 处理仪表板,并希望根据 selectInput 选择的变量来计算新变量。
与普通 R-Code 中的类似:

library(dplyr) 
new_df <- old_df %>% mutate(new_1 = old_var1 + old_var2)

我可以使用 sliderInput 计算新值,但这只是单个值。我想用在表格和图形中显示新变量的所有机会来计算一个新变量。

请尝试以下语法(数据在线可用)。 正如您所提到的,所有输入都在正常工作。

 library(shiny)
 library(readr)
 library(ggplot2)
 library(stringr)
 library(dplyr)
 library(DT)
 library(tools)
 load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))

ui <- fluidPage( 

 sidebarLayout(

# Inputs
sidebarPanel(
  h3("Plotting"),      # Third level header: Plotting

  # Select variable for y-axis 
  selectInput(inputId = "y", 
              label = "Y-axis:",
              choices = c("IMDB rating" = "imdb_rating", 
                          "IMDB number of votes" = "imdb_num_votes", 
                          "Critics Score" = "critics_score", 
                          "Audience Score" = "audience_score", 
                          "Runtime" = "runtime"), 
              selected = "audience_score"),

  # Select variable for x-axis 
  selectInput(inputId = "x", 
              label = "X-axis:",
              choices = c("IMDB rating" = "imdb_rating", 
                          "IMDB number of votes" = "imdb_num_votes", 
                          "Critics Score" = "critics_score", 
                          "Audience Score" = "audience_score", 
                          "Runtime" = "runtime"), 
              selected = "critics_score"),

  # Select variable for color
  selectInput(inputId = "z", 
              label = "Color by:",
              choices = c("Title Type" = "title_type", 
                          "Genre" = "genre", 
                          "MPAA Rating" = "mpaa_rating", 
                          "Critics Rating" = "critics_rating", 
                          "Audience Rating" = "audience_rating"),
              selected = "mpaa_rating"),

  hr(),

  # Set alpha level
  sliderInput(inputId = "alpha", 
              label = "Alpha:", 
              min = 0, max = 1, 
              value = 0.5),

  # Set point size
  sliderInput(inputId = "beta", 
              label = "Beta:", 
              min = 0, max = 5, 
              value = 2)
  ),

# Output:
mainPanel(plotOutput(outputId = "scatterplot"),
          textOutput(outputId = "description"),
          DT::dataTableOutput("moviestable"))
)  
)  
server <- function(input, output, session) {

output$scatterplot <- renderPlot({
ggplot(data = movies, aes_string(x = input$x, y = input$y,
                                          color = input$z)) +
  geom_point(alpha = input$alpha, size = input$beta) +
  labs(x = toTitleCase(str_replace_all(input$x, "_", " ")),
       y = toTitleCase(str_replace_all(input$y, "_", " ")),
       color = toTitleCase(str_replace_all(input$z, "_", " ")))
 })  
vals <- reactiveValues()
observe({
vals$x <- input$alpha
vals$y <- input$beta
vals$sum <- vals$x + vals$y
  })

output$description <- renderText({
paste0("Alpha: ",input$alpha, " Beta:", input$beta," and the sum of alpha and beta:",vals$sum, ".")
})


output$moviestable <- DT::renderDataTable({
DT::datatable(data = movies, 
              options = list(pageLength = 10), 
              rownames = FALSE)
})



}


shinyApp(ui = ui, server = server)

我尝试了不同的方法来解决这个问题:

第一次尝试:

vals2 <- reactiveValues()
observe({
vals2$x <- input$y
vals2$y <- input$x
vals2$sum <- vals2$x + vals2$y
})

output$description2 <- renderText({
paste0("Input y: ",input$y, " Input x:", input$x," and the sum of both variables is:",vals2$sum, ".")
})

警告:+ 中的错误:二元运算符的非数字参数 堆栈跟踪(最内层优先): 56:observerFunc [C:/Users/XXXXXX/Desktop/app.R#110] 1:运行应用程序 错误:[on_request_read] 连接被对等方重置

第二次尝试:

output$try2 <- renderUI({
movies_2 <- movies %>% mutate(new_1 = input$y + input$x)
})

output$moviestable2 <- DT::renderDataTable({
DT::datatable(data = movies_2,
options = list(pageLength = 10),
rownames = FALSE)
})

警告:继承错误:找不到对象“movies_2”

我不知道接下来我可以尝试什么......

我很高兴得到各种帮助!

【问题讨论】:

    标签: r shiny dashboard


    【解决方案1】:

    您应该将movies_2 设置为响应式。您的 output$try2 将不起作用,因为它需要 UI 对象。

    为了匹配您在 UI 端进行的调用,我将其重命名为 moviestable,并将 input$x + input$y 更改为 paste0(input$y, input$x),因为它们都是字符。

    movies_2 <- reactive({
      movies %>% mutate(new_1 := movies[[input$x]] + movies[[input$y]])
    })
    
    output$moviestable <- DT::renderDataTable({
      DT::datatable(data = movies_2(),
        options = list(pageLength = 10),
        rownames = FALSE)
    })
    

    【讨论】:

    • 谢谢,mlegge。我收到此错误:评估错误:二进制运算符的非数字参数。
    • 如果我的回复回答了您的问题,您可以点击投票按钮旁边的绿色复选标记。
    • 感谢您的新回答。现在我不再收到错误消息,但这仍然不是我喜欢做的事情。在您的帮助下,我得到了一个字符串变量,但我喜欢计算一个数字变量。你知道如何计算 x 和 y 之和的数值变量吗?
    • 啊,我理解你,请看我的编辑——也应该有一个 dplyr 方法来做到这一点
    • 就是这样!再次:非常感谢!
    猜你喜欢
    • 2019-03-04
    • 2021-05-17
    • 2016-12-21
    • 1970-01-01
    • 2018-06-22
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多