在ReShape Cast()中使用来自Siny Widget的输入 [英] Using an input from Shiny Widget in Reshape Cast()

查看:14
本文介绍了在ReShape Cast()中使用来自Siny Widget的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我闪亮的Web应用程序中创建一种透视表,用户可以在其中选择他们想要对彼此进行强制转换的变量。我发现的问题是,当我尝试将CAST与我的‘INPUT$COLUMNS’一起使用时,我得到以下结果:错误:强制转换公式包含在熔化数据中找不到的变量:INPUT$COLUMNS。输入$COLUMNS应该是在我的数据集中找到的三个列名之一,所以我不确定问题可能是什么。我附加了代码的简化版本,以尝试隔离问题。

用户界面

 library(shiny)

    shinyUI(fluidPage(

     titlePanel("Reactivity"),


     sidebarLayout(
       sidebarPanel(
       textInput("caption", "Caption:", "Column Name:"),

       selectInput("columns", "Choose a dataset:", 
                  choices = c("Publisher", "Region", "Representative")),

       numericInput("obs", "Number of observations to view:", 10)
      ),


     mainPanel(
      h3(textOutput("caption")), 

      verbatimTextOutput("summary"), 

      dataTableOutput('view')
     )
    )
    ))

服务器

library(shiny)
library(datasets)

shinyServer(function(input, output) {

  datasetInput <- reactive({
    switch(input$columns,
           "Publisher" = Publisher,
           "Region" = Region,
           "Representative" = Representative)
  })

  output$caption <- renderText({
    input$caption
  })

  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  output$view <- renderDataTable({
    test <- input$columns
    m.book.sales <- melt(book.sales)
    cast(m.book.sales, test ~ variable, sum)
  })
})

和数据集

> head(book.sales)
  Representative Region      Month Publisher    Subject Sales Margin Quantity
1            Raj      S 1997-01-01      SAGE Management 135.0  63.45        9
2            Raj      S 1997-01-01 Routledge Statistics 120.0  48.00        6
3            Raj      S 1997-01-01   Penguin  Economics  54.0  22.68        4
4            Raj      S 1997-01-01 Routledge    Fiction 234.0 128.70        9
5           Soni      S 1997-01-01   Penguin   Politics  54.0  22.68        4
6           Soni      S 1997-01-01      SAGE   Politics 185.4 100.12       12

推荐答案

您需要传递一个有效的公式。test是一个变量,不能在公式调用中使用。您可以使用paste构造一个论坛,然后使用as.formula转换:

 output$view <- renderDataTable({
   test <- input$columns
   m.book.sales <- melt(book.sales)
   myFormula <- as.formula(paste0(test, " ~ variable"))
   cast(m.book.sales, myFormula, sum)
 })

这篇关于在ReShape Cast()中使用来自Siny Widget的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆