小编mic*_*404的帖子

如何使用 R 的 xlsx 包对齐 XLSX 文件的单元格?

使用 R 的 xlsx 包创建 XLSX 文件时,默认情况下,包含字符串的列默认向左对齐,包含整数的列向右对齐(包含整数和字符串混合的列也向左对齐)。最终,我想通过将所有列全部向左对齐来标准化所有列,但我在使用 xlsx 时遇到困难。使用下面的示例,如何将所有单元格向左对齐?

library(xlsx)

# Creating dataframe.
df <- data.frame(c(1, 2, 3),
                 c("one", "two", "three"),
                 c("1", "2", "3"))

# Creating a workbook using the XLSX package.
wb <- xlsx::createWorkbook(type = "xlsx")

# Creating a sheet inside the workbook.
sheet <- xlsx::createSheet(wb, sheetName = "Sheet0")

# Adding the full dataset into the sheet.
xlsx::addDataFrame(df, sheet, startRow = 1, startCol = 1, row.names = FALSE, col.names = FALSE)

# Saving the workbook.
xlsx::saveWorkbook(wb, "df.xlsx")
Run Code Online (Sandbox Code Playgroud)

r xlsx

5
推荐指数
1
解决办法
4895
查看次数

如何根据其值更改 R Shiny 数据表单元格的单元格颜色?

我正在尝试根据其值更改 R Shiny 数据表的单元格颜色。例如,我创建了以下应用程序:

# ui.R

fluidPage(
  # Outputting data table.
  DT::dataTableOutput("table")
)

# server.R

library(DT)

data(iris)

function(input, output) {

  # Rendering data table.
  output$table <- DT::renderDataTable({
    head(iris)
  },
  options = list(dom = "t",
                 ordering = FALSE))

}
Run Code Online (Sandbox Code Playgroud)

以下是从上述代码生成的 HTML 框架和结果页面:

R Shiny 生成的 HTML 代码,将 iris 数据集的前 6 行显示为数据表

iris 数据集的前 6 行在 R Shiny 中显示为数据表

例如,假设我希望所有包含整数的单元格都涂成红色。有选择地,我只想为第 2 行第 2 列和第 5 行第 1 列的单元格着色,其中值分别为 3 和 5。这在 R Shiny 中可能吗?

我目前的解决方法是在服务器端设置单个单元格的类,然后用 CSS 为它们着色。但是,我找不到办法做到这一点。

r html-table shiny

4
推荐指数
2
解决办法
7989
查看次数

如何在 R Shiny 应用程序中将变量从模块返回到服务器?

我在 R Shiny 应用程序中将变量从模块返回到服务器时遇到了令人惊讶的困难。在模块中,我想在观察到按下按钮时返回一个值,因此我将return()语句包装在observeEvent(). 但是,没有返回所需的值,整个observeEvent()块似乎是。

我试图创建一个最小的工作示例,概述以下问题:

用户界面

# ui.R

fluidPage(
  input_module_ui("input"),
  actionButton("print_input_button",
               label = "Print Input")
)
Run Code Online (Sandbox Code Playgroud)

服务器

# server.R

function(input, output, session) {

  # Calling input module.
  input_module_return <- callModule(input_module, "input")

  observeEvent(input$print_input_button, {
    print(input_module_return)
  })

}
Run Code Online (Sandbox Code Playgroud)

全局R

# global.R

source("modules/input.R")
Run Code Online (Sandbox Code Playgroud)

输入

# input.R

input_module_ui <- function(id) {

  ns <- NS(id)

  tagList(
    textInput(ns("text_input"),
              label = h2("Input Text:")),
    actionButton(ns("submit_input"),
                 label = "Submit Input")
  )

}

input_module <- function(input, output, session) {

  print("I should only print …
Run Code Online (Sandbox Code Playgroud)

module r shiny

3
推荐指数
1
解决办法
1626
查看次数

标签 统计

r ×3

shiny ×2

html-table ×1

module ×1

xlsx ×1