下面的 MWE 代碼適用于對資料框值求和,用戶可以在底部呈現的“對資料表列求和:”部分的單選按鈕中選擇要分組的周期型別。此分組summed_data()在本server節下面的物件中執行。
但是,我也在嘗試計算 Period_2 == 1 的出現次數。當我注釋掉summed_data()下面當前未注釋的部分,并取消注釋當前已注釋掉的summed_data()以執行唯一行計數,并嘗試運行代碼時,它失敗了。但是,如果我在 R 控制臺中運行這個獨特的行計數函式,如下所示,它可以正常作業并提供所需的結果(手動更改該部分中的“Period...” group_by(...))!
data <- data.frame(
ID = c(115,115,111,88,120,16),
Period_1 = c("2020-01", "2020-02", "2020-03", "2020-01", "2020-02", "2020-03"),
Period_2 = c(1, 2, 3, 1, 1, 4),
ColA = c(1000.01, 20, 30, 40, 50, 60),
ColB = c(15.06, 25, 35, 45, 55, 65)
)
filter(data, Period_2 == "1") %>%
group_by(Period_1) %>%
summarise(count = length(unique(ID)))
Period_1 count
<chr> <int>
1 2020-01 2
2 2020-02 1
所以,對我來說,問題似乎出在下面的 dplyr 代碼中group_by(!!sym(input$grouping))。有沒有人有解決這個問題的建議?
MWE代碼:
library(dplyr)
library(DT)
library(shiny)
library(shinyWidgets)
library(tidyverse)
ui <-
fluidPage(
fluidRow(
column(width = 8,
h3("Data table:"),
tableOutput("data"),
h3("Sum the data table columns:"),
radioButtons(
inputId = "grouping",
label = NULL,
choiceNames = c("By period 1", "By period 2"),
choiceValues = c("Period_1", "Period_2"),
selected = "Period_1",
inline = TRUE
),
DT::dataTableOutput("sums")
)
)
)
server <- function(input, output, session) {
data <- reactive({
data.frame(
ID = c(115,115,111,88,120,16),
Period_1 = c("2020-01", "2020-02", "2020-03", "2020-01", "2020-02", "2020-03"),
Period_2 = c(1, 2, 3, 1, 1, 4),
ColA = c(1000.01, 20, 30, 40, 50, 60),
ColB = c(15.06, 25, 35, 45, 55, 65)
)
})
colNames <- reactive({c(input$grouping, "Col A", "Col B") })
# summed_data <- reactive({
# filter(data(), Period_2 == "1") %>%
# group_by(!!sym(input$grouping)) %>%
# summarise(count = length(unique(ID)))
# })
summed_data <- reactive({
data() %>%
group_by(!!sym(input$grouping)) %>%
select("ColA","ColB") %>%
summarise(across(everything(), sum))
})
output$data <- renderTable(data())
output$sums <- renderDT({
summed_data() %>%
datatable(
rownames = FALSE,
colnames=colNames() # < add colNames()
)
})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
問題colNames()在于您定義并添加到對datatable. 我把這些行注釋掉了,它起作用了。你的問題沒有出現,sum data.frame因為這里colnames實際上存在于 中data.frame,而length(unique)) data.frame.
library(dplyr)
library(DT)
library(shiny)
library(shinyWidgets)
library(tidyverse)
ui <-
fluidPage(
fluidRow(
column(width = 8,
h3("Data table:"),
tableOutput("data"),
h3("Sum the data table columns:"),
radioButtons(
inputId = "grouping",
label = NULL,
choiceNames = c("By period 1", "By period 2"),
choiceValues = c("Period_1", "Period_2"),
selected = "Period_1",
inline = TRUE
),
DT::dataTableOutput("sums")
)
)
)
server <- function(input, output, session) {
mydat <- reactive({
data.frame(
ID = c(115,115,111,88,120,16),
Period_1 = c("2020-01", "2020-02", "2020-03", "2020-01", "2020-02", "2020-03"),
Period_2 = c(1, 2, 3, 1, 1, 4),
ColA = c(1000.01, 20, 30, 40, 50, 60),
ColB = c(15.06, 25, 35, 45, 55, 65)
)
})
# colNames <- reactive({c(input$grouping, "Col A", "Col B") })
summed_data <- reactive({
print(input$grouping)
mydat() %>%
dplyr::filter(Period_2 == 1) %>%
dplyr::group_by(!!sym(input$grouping)) %>%
dplyr::summarise(count = length(unique(ID)))
})
# summed_data <- reactive({
# print(input$grouping)
# data() %>%
# group_by(across(all_of(input$grouping))) %>%
# select("ColA","ColB") %>%
# summarise(across(everything(), sum))
# })
output$data <- renderTable(mydat())
output$sums <- renderDT({
summed_data() %>%
datatable(
rownames = FALSE,
# colnames=colNames() # < add colNames()
)
})
}
shinyApp(ui, server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393425.html
